package com.inwavetech.os.tests.sync.mock;

import com.inwavetech.os.util.SimpleResponse;

import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * Created by jgskin on 3/10/14.
 */
public class SimpleResponseAnswer extends ArrayList<SimpleResponse> implements Answer<SimpleResponse> {

    private Iterator<SimpleResponse> iterator;

    @Override
    public SimpleResponse answer(InvocationOnMock invocation) throws Throwable {
        try {
            return getIterator().next();
        } catch (NoSuchElementException e) {
            return new SimpleResponse(200, "[]");
        }
    }

    public Iterator<SimpleResponse> getIterator() {
        if (iterator == null) {
            iterator = iterator();
        }

        return iterator;
    }

    public void reset() {
        iterator = iterator();
    }
}
